﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace DC2010_Editor
{
    public class CrystalAnimator
    {
        // Fields
        private List<IAnimatedItem> a;
        private List<IAnimatedItem> b;
        private System.Windows.Forms.Timer c;
        private List<IAnimatedItem> d;
        private List<IAnimatedItem> e;

        // Methods
        internal CrystalAnimator()
        {
            //a.b();
            this.c = new System.Windows.Forms.Timer();
            this.c.Tick += new EventHandler(this.OnTick);
            this.a = new List<IAnimatedItem>();
            this.b = new List<IAnimatedItem>();
            this.FramesPerSecond = 30;
        }

        private void OnTick(object A_0, EventArgs A_1)
        {
            this.d = new List<IAnimatedItem>();
            this.e = new List<IAnimatedItem>();
            lock (this.b)
            {
                foreach (IAnimatedItem item in this.b)
                {
                    if (!item.IncreaseFade())
                    {
                        this.e.Add(item);
                    }
                }
                foreach (IAnimatedItem item2 in this.e)
                {
                    this.b.Remove(item2);
                }
            }
            lock (this.a)
            {
                foreach (IAnimatedItem item3 in this.a)
                {
                    if (!item3.DecreaseFade())
                    {
                        this.d.Add(item3);
                    }
                }
                foreach (IAnimatedItem item4 in this.d)
                {
                    this.a.Remove(item4);
                }
            }
            Enabled = ((this.a.Count != 0) || (this.b.Count != 0));
        }

        public void FadeIn(IAnimatedItem item)
        {
            lock (this.a)
            {
                if (this.a.Contains(item))
                {
                    this.a.Remove(item);
                }
            }
            lock (this.b)
            {
                if (!this.b.Contains(item))
                {
                    this.b.Add(item);
                }
            }
            item.IncreaseFade();
            Enabled = true;
        }

        public void FadeOut(IAnimatedItem item)
        {
            lock (this.b)
            {
                if (this.b.Contains(item))
                {
                    this.b.Remove(item);
                }
            }
            lock (this.a)
            {
                if (!this.a.Contains(item))
                {
                    this.a.Add(item);
                }
                Enabled = (this.a.Count != 0);
            }
        }

        // Properties
        private bool Enabled
        {
            get
            {
                return this.c.Enabled;
            }
            set
            {
                if (this.c.Enabled != value)
                {
                    this.c.Enabled = value;
                }
            }
        }

        public int FramesPerSecond
        {
            get
            {
                return (0x3e8 / this.c.Interval);
            }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException("Value '0' is not a valid value for FramesPerSecond. FramesPerSecond must be greater than 0.");
                }
                this.c.Interval = 0x3e8 / value;
            }
        }

    }
}
